package engine.graphics.popup;

import javax.microedition.lcdui.Graphics;

import engine.graphics.MapCanvasEngine;

public abstract class TabPopup extends Popup {

	protected static final byte CLOSE_PANEL = 0;

	protected static int[][][] tabCursor;

	protected byte panel;

	protected boolean hCursorScope;

	protected byte hIndex = CLOSE_PANEL;

	public final void showClosePanel() {
		this.panel = CLOSE_PANEL;
	}

	protected final void hideHCursor() {
		hCursorScope = false;
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		graphics.setColor(128, 128, 128);
		graphics.drawRect(this.x + tabCursor[this.hIndex][0][0], this.y + tabCursor[this.hIndex][0][1],
				tabCursor[this.hIndex][1][0], tabCursor[this.hIndex][1][1]);
	}

	protected final void showHCursor() {
		hCursorScope = true;
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		graphics.setColor(255, 0, 0);
		graphics.drawRect(this.x + tabCursor[this.hIndex][0][0], this.y + tabCursor[this.hIndex][0][1],
				tabCursor[this.hIndex][1][0], tabCursor[this.hIndex][1][1]);
	}

	protected void showPanel() {
		Graphics graphics = MapCanvasEngine.canvas.graphics;
		graphics.setColor(192, 192, 192);
		graphics.fillRect(this.x + 1, this.y + 15, width - 2, height - 15 - 1);
		if (this.hIndex == CLOSE_PANEL)
			showClosePanel();
	}

	final void showNextTab(final byte dX) {
		hideHCursor();
		this.hIndex = (byte) ((this.hIndex + dX + tabCursor.length) % tabCursor.length);
		showHCursor();
		showPanel();
		this.repaint();
	}

	protected final byte moveCursor(final int[][][] vCursor, final byte vIndex, final byte dY, final byte maxLines) {
		byte newVIndex = vIndex;
		if (vCursor.length > 0) {
			if (hCursorScope) {
				hideHCursor();
				showVCursor(vCursor, vIndex);
			} else {
				if (vIndex % maxLines == (maxLines - 1) || vIndex == vCursor.length - 1) {
					hideVCursor(vCursor, vIndex);
					newVIndex -= vIndex % maxLines;
					showHCursor();
				} else
					newVIndex = moveVCursor(vCursor, vIndex, dY);
			}
			this.repaint();
		}
		return newVIndex;
	}
}
